package org.westos.ChinaSoft;

import java.util.Scanner;

public class demo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("1.操作18位身份证号码；2.操作15位身份证号码；");
        System.out.println("请选择：");
        int i = sc.nextInt();
        while (i != 1 && i != 2 ) {
            System.out.println("您的输入有误，请重新输入");
            i = sc.nextInt();
        }
        switch (i) {
            case 1:
                Scanner scanner = new Scanner(System.in);
                System.out.println("请输入18位身份证号码：");
                System.out.println(ShowBirth(scanner.next()));
                break;
            case 2:
                Scanner sc1 = new Scanner(System.in);
                System.out.println("请输入15位身份证号码：");
                String s = ChangeIDCode(sc1.next());
                System.out.println("转换过后生成的18身份证号码为："+s);
                System.out.println(ShowBirth(s));
                break;
        }
    }

    public static String ShowBirth(String s) {
        String sex = "";
        String result = "";
        boolean matches = isIDNumber(s);
        while (true) {
            if (matches || s.length() != 18) {
                result = s.substring(6, 14);
                char c = s.charAt(16);
                Integer num = Integer.valueOf(c);
                if (num % 2 == 0) {
                    sex = "女";
                } else {
                    sex = "男";
                }
                System.out.println("身份证验证通过！");
                break;
            } else {
                System.out.println("->"+!matches);
                Scanner scanner = new Scanner(System.in);
                System.out.println("身份证号码格式不正确，请重新输入:");
                s = scanner.next();
                matches=isIDNumber(s);
            }
        }
        return "您的出生年月为：" + result.substring(0, 4) + "年" + result.substring(4, 6) + "月" + result.substring(6) + "日" + "      " + "性别：" + sex;

    }

    public static boolean isIDNumber(String IDNumber) {
        if (IDNumber == null || "".equals(IDNumber)) {
            return false;
        }
        // 定义判别用户身份证号的正则表达式（15位或者18位，最后一位可以为字母）
        String regularExpression = "(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}[0-9Xx]$)";
        boolean mat = IDNumber.matches(regularExpression);
        System.out.println(mat);
        //判断第18位校验值
        if (!mat) {
            if (IDNumber.length() == 18) {
                try {
                    char[] charArray = IDNumber.toCharArray();
                    //前十七位加权因子
                    int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                    //这是除以11后，可能产生的11位余数对应的验证码
                    String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
                    int sum = 0;
                    for (int i = 0; i < idCardWi.length; i++) {
                        int current = Integer.parseInt(String.valueOf(charArray[i]));
                        int count = current * idCardWi[i];
                        sum += count;
                    }
                    char idCardLast = charArray[17];
                    int idCardMod = sum % 11;
                    if (idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase())) {
                        return true;
                    } else {
//                        System.out.println("身份证最后一位:" + String.valueOf(idCardLast).toUpperCase() +
//                                "错误,正确的应该是:" + idCardY[idCardMod].toUpperCase());
                        System.out.println("身份证号码不正确，请核对后再输：");
                        return false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("异常:" + IDNumber);
                    return false;
                }
            }

        }
        return mat;
    }

    private static String ChangeIDCode(String IDCode) {
        String result = "";
        String IDCode17;
        String msg = "";
        boolean matches = isIDNumber(IDCode);
        while (true) {
            if (!matches || IDCode.length() != 15) {
                Scanner scanner = new Scanner(System.in);
                System.out.println("身份证号码不正确，请重新输入:");
                IDCode = scanner.next();
                matches=isIDNumber(IDCode);
            } else {
                String year2 = IDCode.substring(6, 8);
                Integer year = Integer.valueOf(year2);
                if (year > 20) {
                    IDCode17 = IDCode.substring(0, 6) + "19" + IDCode.substring(6);
                } else {
                    IDCode17 = IDCode.substring(0, 6) + "20" + IDCode.substring(6);
                }
                char[] charArray = IDCode17.toCharArray();
                int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                //这是除以11后，可能产生的11位余数对应的验证码
                String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
                int sum = 0;
                for (int i = 0; i < idCardWi.length; i++) {
                    int current = Integer.parseInt(String.valueOf(charArray[i]));
                    int count = current * idCardWi[i];
                    sum += count;
                }
                int idCardMod = sum % 11;
                String s = idCardY[idCardMod];
                result = IDCode17 + s;
                break;
            }
        }
        return result;
    }
}
